package com.itheima.case2.service.impl;

import com.itheima.case2.dao.PermissionMapper;
import com.itheima.case2.pojo.po.Permission;
import com.itheima.case2.pojo.vo.AddPermission;
import com.itheima.case2.pojo.vo.PageResult;
import com.itheima.case2.pojo.vo.QueryPageBean;
import com.itheima.case2.pojo.vo.UpdatePermission;
import com.itheima.case2.service.PermissionService;
import com.itheima.case2.utils.SqlSessionUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;
import java.util.Map;

public class PermissionServiceImpl implements PermissionService {
    //查询所有权限信息
    @Override
    public List<Permission> queryAllPermission() {
        //获取会话对象
        SqlSession session = SqlSessionUtil.getSession(false);
        //获取接口代理对象
        PermissionMapper mapper = session.getMapper(PermissionMapper.class);
        //调用方法
        List<Permission> list = mapper.queryAllPermission();
        //释放资源
        session.close();
        return list;
    }

    //获取角色分页数据
    @Override
    public PageResult queryPerAndRoleByPage(QueryPageBean pageBean) {
        //获取会话对象
        SqlSession session = SqlSessionUtil.getSession(false);
        //获取接口代理对象
        PermissionMapper mapper = session.getMapper(PermissionMapper.class);
        //获取起始索引和每页记录数   以及模糊查询条件
        Integer startIndex = pageBean.getOffset();
        Integer pageSize = pageBean.getPageSize();
        Map<String,String> queryParams = pageBean.getQueryParams();
        String name = queryParams.get("name");
        //调用对应方法
        List<Permission> permissions = mapper.queryPerAndRoleByPage(startIndex, pageSize, name);
        Long total = mapper.queryAllPermissionCount(name);
        //封装数据到PageResult中
        PageResult pageResult = new PageResult(total,permissions);
        //释放资源
        session.close();
        return pageResult;
    }
    //修改权限和对应角色信息
    @Override
    public void updatePerAndRole(UpdatePermission updatePermission) {
        //获取会话对象
        SqlSession session = SqlSessionUtil.getSession();
        //获取接口代理对象
        PermissionMapper mapper = session.getMapper(PermissionMapper.class);
        //调用方法
        mapper.updatePermission(updatePermission);
        mapper.deleteById(updatePermission.getId().toString());
        List<String> roleIds = updatePermission.getRoleIds();
        for (String roleId : roleIds) {
            mapper.insertPerAndRole(roleId,updatePermission.getId().toString());
        }
        //释放资源
        session.close();
    }

    //添加权限和对应角色信息
    @Override
    public void insertPerAndRole(AddPermission addPermission) {
        //获取会话对象
        SqlSession session = SqlSessionUtil.getSession();
        //获取接口代理对象
        PermissionMapper mapper = session.getMapper(PermissionMapper.class);
        //调用方法
        mapper.insertPermission(addPermission);
        List<String> roleIds = addPermission.getRoleIds();
        for (String roleId : roleIds) {
            mapper.insertPerAndRole(roleId,addPermission.getId().toString());
        }
        //释放资源
        session.close();
    }
    //删除权限和对应角色信息
    @Override
    public void deleteById(String roleId) {
        //获取会话对象
        SqlSession session = SqlSessionUtil.getSession();
        //获取接口代理对象
        PermissionMapper mapper = session.getMapper(PermissionMapper.class);
        //调用方法
        mapper.deleteById(roleId);
        mapper.deletePermission(roleId);
        //释放资源
        session.close();
    }


}
